﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CRM.Component.Business;
using TRPOP.CRM.Component.Common;
using TRPOP.CRM.Component.Model;

namespace TRPOP.CRM.Web
{
    public partial class Company_followingList : BaseAdmin
    {
        #region 权限限制
        protected override void SubPageInit()
        {
            base.m_oCurrentPermissionKey = "Company2_followingList";
        }
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
                BindData();
            }
        }
        /// <summary>
        /// 分页触发此事件（分页采用post方法传递数据）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void UC_PagerChanged(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strCondation = string.Empty;

            if (!txtName.Text.IsEmpty() && txtName.Text != "输入关键字")
            {
                strCondation = string.Format(" AND (  nvcCompanyName LIKE '%{0}%'  OR nvcRegisterAdress LIKE '%{0}%'  OR nvcContactPhone LIKE '%{0}%' OR nvcContactName LIKE '%{0}%'  OR nvcEmployeeName  LIKE '%{0}%' )  ", txtName.Text.Trim().HtmlEncode().SQLFilter());
            }

            ViewState["SearchCondation"] = strCondation;

            Pager.Page_ReLoad();

            BindData();
        }
        /// <summary>
        /// 批量添加客户分配计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strRegiterIds = RequestHelper.RequestString("delid");

            if (strRegiterIds.IsEmpty())
            {
                return;
            }


            bool bIsDistribute = false;
            CompanyInfo oCompanyInfo;
            int iEmployeeID = ddlEmployee.SelectedValue.ToInt();

            if (iEmployeeID > 0)
            {
                foreach (string strID in strRegiterIds.Split(','))
                {
                    oCompanyInfo = CompanyManage.GetCompanyInfo(strID.ToInt());

                    if (oCompanyInfo != null)
                    {
                        oCompanyInfo.iCompanyStatus = 1;
                        oCompanyInfo.iEmployeeFollowup = ddlEmployee.SelectedValue.ToInt();//base.GetCurrentEmployeeInfo().iEmployeeID;
                        if (CompanyManage.SaveCompanyInfo(oCompanyInfo))
                            bIsDistribute = true;
                    }
                }
            }
            else
            {
                foreach (string strID in strRegiterIds.Split(','))
                {
                    oCompanyInfo = CompanyManage.GetCompanyInfo(strID.ToInt());

                    if (oCompanyInfo != null)
                    {
                        oCompanyInfo.iCompanyStatus = 2;
                        if (CompanyManage.SaveCompanyInfo(oCompanyInfo))
                            bIsDistribute = true;
                    }
                }
            }

            if (bIsDistribute)
            {
                Alert("操作成功");
            }
            else
            {
                Alert("操作失败");
            }

            Pager.Page_ReLoad();
            BindData();
        }
        #endregion

        #region 页面方法
        void LoadBase()
        {
            //ddlEmployee.DataSource =

            //CompanyManage.UpdateRemindCompany();
            int intCount = 0;

            string strCondation = string.Format(" AND bIsDelete!={0} ", 1);

            ddlEmployee.DataSource = EmployeeManage.GetEmployeeSimpleInfoPager(1, int.MaxValue, strCondation, "iEmployeeID ASC", out intCount);

            ddlEmployee.DataTextField = "nvcEmployeeName";
            ddlEmployee.DataValueField = "iEmployeeID";

            ddlEmployee.DataBind();

            ddlEmployee.Items.Insert(0,new ListItem("公海", "0"));
        }
        /// <summary>
        /// 绑定管理员信息列表
        /// </summary>
        protected void BindData()
        {
            int RecordCount = 0;

            string strCondition = string.Empty;
            if (ViewState["SearchCondation"] != null)
            {
                strCondition = ViewState["SearchCondation"].ToString();
            }

            strCondition += " AND iCompanyStatus=" + 1;

            rptList.DataSource = CompanyManage.GetRemindExtraInfoListByPager(Pager.CurrentPage, Pager.PageSize, strCondition, string.Empty, out RecordCount);

            rptList.DataBind();

            Pager.RecordCount = RecordCount;
        }
        #endregion
    }
}